<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>public/skin/back-end/css/tablemain.css"/>	
	<?php $this->load->helper('form');?>
	<?php 
	$dir = getcwd().'/application/views/front/';

// Lay noi dung trong thu muc dua vao mot mang
$fileList = scandir($dir) or die ("Not a directory");

// In ra ten va kich thuoc tap tin

$templateList = array();
foreach ($fileList as $file) {
    if (is_file("$dir/$file") && $file != '.' && $file != '..') {
    	$file = substr($file, 0, strpos($file, '.'));
        $templateList[$file]=$file;
    }
}
	?>
	<h1>Page Management <span class="greysubheading">Edit Page</span></h1> 
	<?php echo (!empty($message))?$message:null;?>
	
	<form id="content-edit-form" method="post" action="">
		<table class="page-tbl" width="100%" cellpadding="0" cellspacing="0">    
		<thead> 
          <tr> 
            <th scope="col">Fields</th> 
            <th scope="col">Data's</th> 
          </tr> 
        </thead>
		<tbody class="elements_container">
	        <?php echo textfield('title','Title',$page['title'])?>
	        <?php echo textfield('page_heading','Page heading',$page['page_heading'])?>
	        <?php echo plaintext('description','Description',$page['description'])?>
	        <?php echo textfield('url','Url',$page['url'])?>
	        <?php echo dropdown('language','Language',$langList, $page['language'])?>
	        <?php
		        require 'application/controllers/front/front_controller.php';
				$actionList = array();
				foreach(get_class_methods('front_controller') as $method){
					if($method!='_remap'&&!in_array($method, get_class_methods('MY_Controller'))&&!in_array($method, get_class_methods('CI_Controller')))
						$actionList[$method] = ucwords(str_replace('_',' ', $method));
				} 
		        echo dropdown('action','Action',$actionList,$page['action']);
	        ?>
	        <?php echo dropdown('template','Template',$templateList,$page['template'],'class="template_field"')?>
	        <?php foreach($page['elements'] as $row){
	        		echo $row;
	        }?>
        </tbody>
        <tfoot> 
          <tr> 
            <th scope="row"></th> 
            <td><!--pagination --> 
              <div id="central" align="center">
                  <table> 
                    <tr> 
                      <td><input type="submit" id="button" value="Edit" /></td> 
                      <td><input type="reset" id="reset" value="Reset" onClick="location.reload(true)"/></td> 
                    </tr> 
                  </table> 
              </div></td> 
          </tr> 
        </tfoot>
      </table>
	</form>
<script language="JavaScript" type="text/javascript">    
$(document).ready(function() {
	$('.template_field').change(function(){
		$.ajax({
		      url: "<?php echo base_url();?>admin/page_management/add_elements",
		      type: "POST",
		      data: ({template : $(this).val(),page_id : <?php echo $page['id']?>}),
		      dataType: "html",
		      success: function(data, textStatus, jqXHR){
		    	
				$(".elements:gt(6)").remove();
				for ( keyVar in CKEDITOR.instances ) {
					CKEDITOR.remove(CKEDITOR.instances[keyVar]);
				}
				$(".elements_container").append(data);
		      }
		});
	});
	$('#content-edit-form').submit(function() {
		for ( keyVar in CKEDITOR.instances ) {
			CKEDITOR.instances[keyVar].updateElement();
		}
		
	});						
});	
</script>